package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.product.ProductCollection;
import com.zuipin.entity.product.ProductExtInfo;
import com.zuipin.entity.product.ProductInfo;
import com.zuipin.entity.product.ProductPrice;
import com.zuipin.entity.product.TagInfo;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.ProductCollectionMapper;
import com.zuipin.mapper.ProductExtInfoMapper;
import com.zuipin.mapper.ProductInfoMapper;
import com.zuipin.mapper.ProductPriceMapper;
import com.zuipin.service.IProductCollectionService;
import com.zuipin.service.ITagInfoService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.IntegralProVo;

import net.sf.json.JSONObject;

/**
 * 商品集合业务类实现
 *
 * @ClassName ProductCollectionServiceImpl 
 * @author wangcongyi
 * @date 2017年7月3日 上午10:30:04
 */
@Service
public class ProductCollectionServiceImpl implements IProductCollectionService {
	
	@Resource
	private ProductCollectionMapper	productCollectionMapper;
	
	@Resource
	private ProductPriceMapper		productPriceMapper;
	
	@Resource
	private ProductExtInfoMapper	productExtInfoMapper;
	
	@Resource
	private ProductInfoMapper		productInfoMapper;
	
	@Resource
	private ITagInfoService			tagInfoService;
	
	private final static String		IMG_URL	= "IMG_URL";
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return productCollectionMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public int insert(ProductCollection record) {
		// TODO Auto-generated method stub
		return productCollectionMapper.insert(record);
	}
	
	@Override
	public int insertSelective(ProductCollection record) {
		// TODO Auto-generated method stub
		// 获取当前时间
		String currentTime = DateUtils.getCurrentDateTime();
		record.setCreateTime(currentTime);
		return productCollectionMapper.insertSelective(record);
	}
	
	@Override
	public ProductCollection selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return productCollectionMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int updateByPrimaryKeySelective(ProductCollection record) {
		// TODO Auto-generated method stub
		// 获取当前时间
		String currentTime = DateUtils.getCurrentDateTime();
		record.setUpdateTime(currentTime);
		return productCollectionMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int updateByPrimaryKey(ProductCollection record) {
		// TODO Auto-generated method stub
		return productCollectionMapper.updateByPrimaryKey(record);
	}
	
	@Override
	public List<ProductCollection> findProductCollection(ProductCollection collection, Pagination page) {
		// TODO Auto-generated method stub
		return productCollectionMapper.findProductCollection(collection, page);
	}
	
	@Override
	public String getMaxNo() {
		// TODO Auto-generated method stub
		return productCollectionMapper.getMaxNo();
	}
	
	@Override
	public ProductCollection isExists(ProductCollection collection) {
		// TODO Auto-generated method stub
		return productCollectionMapper.isExists(collection);
	}
	
	@Override
	public boolean modifyState(ProductCollection collection) {
		// TODO Auto-generated method stub
		return productCollectionMapper.modifyState(collection);
	}
	
	@Override
	public List<ProductCollection> getProCollectionByBack(String seachCondition, Long sysId, Pagination page) {
		// TODO Auto-generated method stub
		return productCollectionMapper.getProCollectionByBack(seachCondition, sysId, page);
	}
	
	public Result<List<IntegralProVo>> getHotExchangeProCollection(Integer isDisplay, String collectionNo, String device, Long sysId, Pagination page) {
		PageResult<List<IntegralProVo>> result = new PageResult<>();
		List<IntegralProVo> vos = new ArrayList<>();
		// 创建作为查询条件的 商品集合对象
		ProductCollection collection = new ProductCollection();
		collection.setSysId(sysId);
		collection.setState(ProductCollection.NORMAL);
		collection.setCollectionNo(collectionNo);
		
		// 通过集合的名称查询出热门兑换商品的信息
		// 分页不是分商品集合 是分集合中的商品
		Pagination pages = new Pagination();
		List<ProductCollection> list = findProductCollection(collection, pages);
		
		// 查询出的集合信息
		ProductCollection pc = null;
		
		if (list != null && !list.isEmpty()) {
			pc = list.get(0);
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("未找到商品集合信息");
			return result;
		}
		
		List<String> skus = StringUtil.splitString(pc.getProSkus());
		
		Iterator<String> it = skus.iterator();
		// 填充IntegralProVo信息
		while (it.hasNext()) {
			String sku = it.next();
			Map<String, Object> map = productExtInfoMapper.findProAndMainUrlImgByPro(sku, device, sysId);
			// 填充图片地址信息
			String mainImgUrl = "";
			String title = "";
			if (map.get(IMG_URL) != null) {
				mainImgUrl = (String) map.get(IMG_URL);
			}
			if (map.get(IntegralProVo.TITLE) != null) {
				title = (String) map.get(IntegralProVo.TITLE);
			}
			ProductExtInfo proExtInfo = productExtInfoMapper.findProductExtInfoBySku(sku, sysId);
			
			ProductInfo productInfo = productInfoMapper.selectByProductProSku(sku);
			// 是否显示未上架的商品信息
			if (isDisplay.equals(ProductCollection.NOT_DISPLAY)) {
				if (proExtInfo.getState().equals(ProductExtInfo.OFF_SHELF)) {
					it.remove();
					continue;
				}
			}
			IntegralProVo vo = new IntegralProVo();
			vo.setIsIntegralPro(productInfo.getIsExchangeIntegral());
			vo.setProId((Long) map.get(IntegralProVo.PROID));
			vo.setProSku(sku);
			vo.setMainImgUrl(mainImgUrl);
			vo.setTitle(title);
			vo.setProExtId(proExtInfo.getId());
			vo.setAssessmentNum(proExtInfo.getAssessmentNum());
			vo.setSubTitle(proExtInfo.getSubTitle());
			vo.setIsSales(proExtInfo.getIsSales());
			if (ProductExtInfo.IS_SPECIAL.equals(proExtInfo.getIsSales())) {
				vo.setSalesPrice(proExtInfo.getSalesPrice());
			}
			vo.setStockNum(proExtInfo.getStockNum());
			vo.setTagId(proExtInfo.getTagId());
			
			TagInfo tagInfo = tagInfoService.selectByPrimaryKey(proExtInfo.getTagId());
			if (tagInfo != null) {
				vo.setTagName(tagInfo.getName());
				vo.setTagPcImgUrl(tagInfo.getPcImgUrl());
				vo.setTagWapImgUrl(tagInfo.getWapImgUrl());
			}
			
			vo.setIsSoldOut(proExtInfo.getIsSoldOut());
			if (tagInfo != null && TagInfo.ENABLED.equals(tagInfo.getState())) {
				vo.setTagName(tagInfo.getName());
				vo.setTagPcImgUrl(tagInfo.getPcImgUrl());
				vo.setTagWapImgUrl(tagInfo.getWapImgUrl());
			}
			// 填充积分信息
			JSONObject param = new JSONObject();
			param.put("proSku", sku);
			param.put("sysId", sysId);
			
			// 查询出商品价格类型列表
			List<ProductPrice> prices = productPriceMapper.findByProSku(param);
			for (ProductPrice price : prices) {
				// 查出为积分类型
				if (ProductPrice.INTEGRAL.equals(price.getPriceType()) && ProductInfo.YES_EXCHANGE_INTEGRAL.equals(productInfo.getIsExchangeIntegral())) {
					vo.setIntegralPrice(price.getPrice().intValue());
				}
				if (ProductPrice.ZUIPIN.equals(price.getPriceType()) && ProductInfo.NO_EXCHANGE_INTEGRAL.equals(productInfo.getIsExchangeIntegral())) {
					vo.setProPrice(price.getPrice());
				}
				if (ProductPrice.MARKET.equals(price.getPriceType())) {
					vo.setMarketPrice(price.getPrice());
				}
			}
			vos.add(vo);
		}
		
		// 排序 有货的在前面
		vos.sort((h1, h2) -> h2.getIsSoldOut().compareTo(h1.getIsSoldOut()));
		
		// 分页===========================
		List<IntegralProVo> proList = new ArrayList<IntegralProVo>();
		int currIdx = (page.getPageNumber() > 1 ? (page.getPageNumber() - 1) * page.getPageSize() : 0);
		for (int i = 0; i < page.getPageSize() && i < vos.size() - currIdx; i++) {
			IntegralProVo integralPro = vos.get(currIdx + i);
			proList.add(integralPro);
		}
		result.setData(proList);
		// 分页===========================
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setTotal_record(skus.size());
		return result;
		
	}
	
}
